package com.yan.deezer.network;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.client.methods.HttpGet;

public class RequestBuilder {

	private static final String TAG = "RequestBuilder";

	private static final String ROOT_URL = "http://api.deezer.com/2.0/";

	private static final String REQUEST_ACTION_SEARCH_TRACKS = "search?q=";
	private static final String REQUEST_ACTION_SEARCH_ARTISTS = "search/artist?q=";
	private static final String REQUEST_ACTION_SEARCH_ALBUMS = "search/album?q=";
	private static final String REQUEST_ACTION_GET_ARTIST = "artist/";
	private static final String REQUEST_ACTION_GET_ALBUMS = "/albums";
	private static final String REQUEST_ACTION_GET_ALBUM = "album/";
	private static final String REQUEST_ACTION_GET_TRACKS = "/tracks";

	// http://api.deezer.com/2.0/album/302127/tracks

	// artist/27/albums

	private static StringBuffer rootQuery() {
		return new StringBuffer().append(ROOT_URL);
	}

	private static StringBuffer basicQuery(String action) {
		StringBuffer query = rootQuery();

		query.append(action);
		return query;
	}

	public static HttpGet getTracksQuery(String userInput) {
		StringBuffer query = basicQuery(REQUEST_ACTION_SEARCH_TRACKS);
		query.append(userInput);
		return getHttpRequest(encodeURIcomponent(query.toString()));

	}

	public static HttpGet getAlbumsQuery(String userInput) {
		StringBuffer query = basicQuery(REQUEST_ACTION_SEARCH_ALBUMS);
		query.append(userInput);
		return getHttpRequest(encodeURIcomponent(query.toString()));
	}

	public static HttpGet getArtistsQuery(String userInput) {
		StringBuffer query = basicQuery(REQUEST_ACTION_SEARCH_ARTISTS);
		query.append(userInput);
		return getHttpRequest(encodeURIcomponent(query.toString()));
	}

	public static HttpGet getArtistQuery(String artistID) {
		StringBuffer query = basicQuery(REQUEST_ACTION_GET_ARTIST);
		query.append(artistID);
		return getHttpRequest(encodeURIcomponent(query.toString()));
	}

	public static HttpGet getArtistAlbumsQuery(String artistID) {
		StringBuffer query = basicQuery(REQUEST_ACTION_GET_ARTIST);
		query.append(artistID).append(REQUEST_ACTION_GET_ALBUMS);
		return getHttpRequest(encodeURIcomponent(query.toString()));
	}

	public static HttpGet getAlbumTracksQuery(String albumID) {
		StringBuffer query = basicQuery(REQUEST_ACTION_GET_ALBUM);
		query.append(albumID).append(REQUEST_ACTION_GET_TRACKS);
		return getHttpRequest(encodeURIcomponent(query.toString()));
	}

	private static HttpGet getHttpRequest(String url) {
		HttpGet request = new HttpGet(url);

		return request;
	}

	/** Converts a string into something you can safely insert into a URL. */
	public static String encodeURIcomponent(String s) {
		URL url = null;
		try {
			url = new URL(s);
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		URI uri = null;
		try {
			uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
					url.getPort(), url.getPath(), url.getQuery(), url.getRef());
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			url = uri.toURL();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url.toString();
	}

}